class MyBlocking{
    //创建一个数组
    private int[] data = new int[1000];
    //阻塞队列的头结点
    private int head = 0;
    //阻塞队列的尾结点
    private int tail = 0;
    //阻塞队列的总结点
    private int size = 0;

    //阻塞队列的入队列
    public void put (int value) throws InterruptedException {
        synchronized (this) {
            if (size == data.length) {
                this.wait();
            }
            data[tail] = value;
            tail++;
            if (tail >= data.length) {
                tail = 0;
            }
            size++;
            this.notify();
        }
    }
    public Integer take() throws InterruptedException {
        synchronized (this) {
            if (size == 0) {
                this.wait();
            }
            int num = data[head];
            head++;
            if (head >= data.length) {
                head = 0;
            }
            size--;
            this.notify();
            return num;
        }
    }
}
public class Test {
    public static MyBlocking queue = new MyBlocking();
    public static void main(String[] args) {
        Thread produce = new Thread(() ->{
            int num = 0;
            while(true){
                try {
                    System.out.println("生产了：" + num);
                    queue.put(num);
                    num++;
                    //Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        produce.start();

        Thread custom = new Thread(() ->{
           while(true){
               try {
                   int ret = queue.take();
                   System.out.println("消费了: " + ret);
                   Thread.sleep(500);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        });
        custom.start();
    }
}
